﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise.Core
// GenericCommand.cs
// 
// Author: Matt Van Horn
// Created 11/16/2011 12:33 PM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System;
using System.Diagnostics;
using System.Windows.Input;

namespace SilverlightInEnterprise.Core.ViewModels
{
    public delegate void Execute(object parameter);

    public delegate bool CanExecute(object parameter);

    [DebuggerNonUserCode]
    public class GenericCommand : ICommand
    {
        private Execute _executeMethod;

        private CanExecute _canExecuteMethod;

        #region Constructors

        [DebuggerStepThrough]
        public GenericCommand()
            : this(null, null)
        {
        }

        [DebuggerStepThrough]
        public GenericCommand(Execute executeMethod)
            : this(executeMethod, null)
        {
        }

        [DebuggerStepThrough]
        public GenericCommand(Execute executeMethod, CanExecute canExecuteMethod)
        {
            _executeMethod = executeMethod;
            _canExecuteMethod = canExecuteMethod;
        }

        #endregion

        #region Methods

        [DebuggerStepThrough]
        public void ChangeCanExecute()
        {
            if (this.CanExecuteChanged == null) return;
            this.CanExecuteChanged(this, EventArgs.Empty);
        }

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            if (_canExecuteMethod == null)
            {
                return true;
            }
            else
            {
                return _canExecuteMethod(parameter);
            }
        }

        public event EventHandler CanExecuteChanged;

        [DebuggerStepThrough]
        public void Execute(object parameter)
        {
            if (_executeMethod != null)
            {
                _executeMethod(parameter);
            }
        }

        #endregion
    }
}